﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SchoolManagementSystem
{
    class Utilities
    {
        public static string getDateInIndonesia(DateTime time)
        {
            string data = null;

            data = time.Day.ToString();
            switch (time.Month)
            {
                case 1:
                    data += " Januari ";
                    break;

                case 2:
                    data += " Februari ";
                    break;

                case 3:
                    data += " Maret ";
                    break;

                case 4:
                    data += " April ";
                    break;

                case 5:
                    data += " Mei ";
                    break;

                case 6:
                    data += " Juni ";
                    break;

                case 7:
                    data += " Juli ";
                    break;

                case 8:
                    data += " Agustus ";
                    break;

                case 9:
                    data += " September ";
                    break;

                case 10:
                    data += " Oktober ";
                    break;

                case 11:
                    data += " November ";
                    break;

                case 12:
                    data += " Desember ";
                    break;

                default:
                    break;
            }

            data += time.Year.ToString();

            return data;
        }

        private static int toInt(char c)
        {
            return (int)(c - '0');
        }

        private static Boolean terbilang_get_valid(string str, int from, int to, int min=1, int max=9)
        {
            Boolean val = false;

            from = from < 0 ? 0 : from;
            for (int i = from; i < to; i++)
            {
                int test = toInt(str[i]);

                if ((toInt(str[i]) >= min) && (toInt(str[i]) <= max))
                {
                    val = true;
                }
            }
           
            return val;
        }

        private static string terbilang_get_str(int i, string str, int len)
        {
            string[] arrayA = new string[10] {"", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan"};
            string[] arrayB = new string[10] {"", "Se", "Dua ", "Tiga ", "Empat ", "Lima ", "Enam ", "Tujuh ", "Delapan ", "Sembilan "};
            string[] arrayC = new string[10] {"", "Se", "Dua ", "Tiga ", "Empat ", "Lima ", "Enam ", "Tujuh ", "Delapan ", "Sembilan " };
            string[] arrayD = new string[15];

            arrayD.SetValue("puluh", 0);
            arrayD.SetValue("belas", 1);
            arrayD.SetValue("ratus", 2);
            arrayD.SetValue("ribu", 4);
            arrayD.SetValue("juta", 7);
            arrayD.SetValue("milyar", 10);
            arrayD.SetValue("triliyun", 13);

            string buf = null;
            int pos = len - i;
            switch (pos)
            {
                case 1:
                    if (!terbilang_get_valid(str, i - 1, 1, 1))
                    {
                        buf = arrayA[toInt(str[i])];
                    }      
                    break;

                case 2:
                case 5:
                case 8:
                case 11:
                case 14:
                    int value = toInt(str[i]);
                    if (toInt(str[i]) == 1)
                    {
                        if (toInt(str[i + 1]) == 0)
                        {
                            buf = arrayB[toInt(str[i])] + arrayD[0];
                        }
                        else
                        {
                            buf = arrayB[toInt(str[i+1])] + arrayD[1];
                        }
                    }
                    else if (toInt(str[i]) > 1)
                    {
                        buf = arrayB[toInt(str[i])] + arrayD[0];
                    }
                    break;

                case 3:
                case 6:
                case 9:
                case 12:
                case 15:
                    int value2 = toInt(str[i]);
                    if (toInt(str[i]) > 0)
                    {
                        buf = arrayB[toInt(str[i])] + arrayD[2];
                    }
                    break;

                case 4:
                case 7:
                case 10:
                case 13:
                    if (terbilang_get_valid(str, i - 2, i))
                    {
                        if (!terbilang_get_valid(str, i - 1, i, 1, 1))
                        {
                            buf = arrayC[toInt(str[i])] + arrayD[pos];
                        }
                        else
                        {
                            buf = arrayD[pos];
                        }
                    }
                    else if (toInt(str[i]) > 0)
                    {
                        if (pos == 4)
                        {
                            buf = arrayB[toInt(str[i])] + arrayD[pos];
                        }
                        else
                        {
                            buf = arrayC[toInt(str[i])] + arrayD[pos];
                        }
                    }
                    break;

                default:
                    break;
            }
            return buf;
        }

        public static string getNumberToWordInIndonesia(string nominal)
        {
            string[] arrayA = new string[10] { "", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan" };
            string[] arrayB = new string[10] { "", "se", "dua ", "tiga ", "empat ", "lima ", "enam ", "tujuh ", "delapan ", "sembilan " };
            string[] arrayC = new string[10] { "", "satu ", "dua ", "tiga ", "empat ", "lima ", "enam ", "tujuh ", "delapan ", "sembilan " };
            string[] arrayD = new string[15];

            arrayD.SetValue("puluh", 0);
            arrayD.SetValue("belas", 1);
            arrayD.SetValue("ratus", 2);
            arrayD.SetValue("ribu", 4);
            arrayD.SetValue("juta", 7);
            arrayD.SetValue("milyar", 10);
            arrayD.SetValue("triliyun", 13);

            string word = null;

            string str = nominal;
            int len = nominal.ToString().Length;

            for (int i = 0; i < len; i++)
            {
                int pos = len - i;
                string buf = null;
                switch (pos)
                {
                    case 1:
                        if ((toInt(str[i]) >= 1) && (toInt(str[i]) <= 9))
                        {
                            if ((len == 2) && (toInt(str[i]) == 0))
                            {
                                // Do not write satuan
                            }
                            else if ((len > 2) && (toInt(str[i - 1]) == 1))
                            {
                                // Do not write satuan
                            }
                            else
                            {
                                buf = arrayA[toInt(str[i])];
                            }


                            /* 
                            if ((len == 2) && (toInt(str[i]) == 0))
                            {
                                // Do not write satuan
                            }
                            else if ((len == 3) && (toInt(str[i-1]) == 1))
                            {
                                // Do not write satuan
                            }
                            else if ((len == 4) && (toInt(str[i - 1]) == 1))
                            {
                                // Do not write satuan
                            }
                            else if ((len == 5) && (toInt(str[i - 1]) == 1))
                            {
                                // Do not write satuan
                            }
                            else
                            {
                                buf = arrayA[toInt(str[i])];
                            }
                             * */ 
                        }
                        break;

                    case 2:
                    case 5:
                    case 8:
                    case 11:
                    case 14:
                        int value = toInt(str[i]);
                        // Jika puluhan angka 1
                        if (toInt(str[i]) == 1)
                        {
                            // Jika satuannya angka 0
                            if (toInt(str[i + 1]) == 0)
                            {
                                buf = arrayB[toInt(str[i])] + arrayD[0];
                            }
                            // Jika satuannya selain angka 0
                            else
                            {
                                buf = arrayB[toInt(str[i + 1])] + arrayD[1];
                            }
                        }
                        // Jika puluhan diatas 1 (2, 3, ....)
                        else if (toInt(str[i]) > 1)
                        {
                            buf = arrayB[toInt(str[i])] + arrayD[0];
                        }
                        break;

                    case 3:
                    case 6:
                    case 9:
                    case 12:
                    case 15:
                        int value2 = toInt(str[i]);
                        if (toInt(str[i]) > 0)
                        {
                            buf = arrayB[toInt(str[i])] + arrayD[2];
                        }
                        break;

                    case 4:
                    case 7:
                    case 10:
                    case 13:
                        int value3 = toInt(str[i]);
                        if (terbilang_get_valid(str, i - 2, i))
                        {
                            if (!terbilang_get_valid(str, i - 1, i, 1, 1))
                            {
                                buf = arrayC[toInt(str[i])] + arrayD[pos];
                            }
                            else
                            {
                                buf = arrayD[pos];
                            }
                        }
                        else if (toInt(str[i]) > 0)
                        {
                            if (pos == 4)
                            {
                                buf = arrayB[toInt(str[i])] + arrayD[pos];
                            }
                            else
                            {
                                buf = arrayC[toInt(str[i])] + arrayD[pos];
                            }
                        }
                        break;

                    default:
                        break;
                }

                word += buf;
                word += " ";
            }

            word += " rupiah";

            return word;



            /* 
            string word = null;

            string str = nominal;
            int len = nominal.ToString().Length;

            for (int i = 0; i < len; i++)
            {
                string buff = terbilang_get_str(i, str, len);
                word += buff;
                word += " ";
            }

            word += " Rupiah";

            return word;
             * */ 
        }
    }
}


